package com.wontube.smr.dao.bo;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.core.dao.mybatis.QueryExample;
import com.wontube.smr.dao.mapper.web.FeedbackMapper;
import com.wontube.smr.dao.mapper.web.FeedbackReplyMapper;
import com.wontube.smr.pub.DefaultDBHelper;
import com.wontube.smr.web.client.model.po.Feedback;
import com.wontube.smr.web.client.model.po.FeedbackReply;
import com.wontube.smr.web.client.model.vo.FeedbackItems;

/**
 * @author tanggf(2012-5-7)
 *
 */
public class FeedbackDao {
	/**
	 * select valid feedback
	 * @param pageInfo
	 * @return
	 */
	public static FeedbackItems selectValidFeedbacks(PagenateInfo pageInfo,Integer feedbackType){
		return selectFeedbacks(pageInfo, Feedback.STATUS_NORMAL,feedbackType);
	}
	
	/**
	 * select all feedback
	 * @param pageInfo
	 * @return
	 */
	public static FeedbackItems selectAllFeedbacks(PagenateInfo pageInfo){
		return selectFeedbacks(pageInfo, null, null);
	}
	
	/**
	 * select deleted feedback
	 * @param pageInfo
	 * @return
	 */
	public static FeedbackItems selectDeletedFeedbacks(PagenateInfo pageInfo){
		return selectFeedbacks(pageInfo, Feedback.STATUS_DELETED, null);
	}
	
	public static FeedbackItems selectFeedbacks(PagenateInfo pageInfo, Integer status, Integer feedbackType){
		FeedbackItems items = new FeedbackItems(pageInfo);
		QueryExample ex = new QueryExample();
		if(status != null){
			ex.or().addCriterion("status=", status)
				.addCriterion("feed_type=",feedbackType);
		}				
		
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {			
			FeedbackMapper mapper = session.getMapper(FeedbackMapper.class);
			RowBounds rowBounds = new RowBounds(pageInfo.getOffset(), pageInfo.getLimit());
			//list
			List<Feedback> feedbacks = mapper.selectFeedbacksWithReply(status, feedbackType, rowBounds);
			
			items.setItems(feedbacks);
			
			//count
			items.setTotalCount(mapper.countByExample(ex));
		} finally {
			session.close();
		}
		
		return items;
	}
	
	public static int insertFeedback(Feedback feedback){		
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			FeedbackMapper mapper = session.getMapper(FeedbackMapper.class);
			return mapper.insert(feedback);
		} finally {
			session.close();
		}
	}
	
	/**
	 * update status to deleted
	 * @param ids
	 * @return
	 */
	public static int deleteFeedback(String[] ids){
		SqlSession session = DefaultDBHelper.getSqlSession(false);
		try {
			FeedbackMapper mapper = session.getMapper(FeedbackMapper.class);
			int aff = 0;
			for (String id : ids) {
				Feedback record = new Feedback();
				record.setId(id);
				record.setStatus(Feedback.STATUS_DELETED);
				aff += mapper.updateByPrimaryKeySelective(record);				
			}
			return aff;
		} finally {
			session.commit();
			session.close();
		}
	}
	
	public static int undeleteFeedback(String fid){
		SqlSession session = DefaultDBHelper.getSqlSession(false);
		try {
			FeedbackMapper mapper = session.getMapper(FeedbackMapper.class);
			Feedback record = new Feedback();
			record.setId(fid);
			record.setStatus(Feedback.STATUS_NORMAL);
			return mapper.updateByPrimaryKeySelective(record);		
		} finally {
			session.commit();
			session.close();
		}
	}
	
	public static int addFeedbackReply(FeedbackReply reply){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			FeedbackReplyMapper mapper = session.getMapper(FeedbackReplyMapper.class);
			return mapper.insert(reply);
		} finally {			
			session.close();
		}
	}
	
	/**
	 * deleted feedbackReply
	 * @param ids
	 * @return
	 */
	public static int deleteFeedbackReply(String id){
		SqlSession session = DefaultDBHelper.getSqlSession(false);
		try {
			FeedbackReplyMapper mapper = session.getMapper(FeedbackReplyMapper.class);
			int aff = 0;
			aff += mapper.deleteByPrimaryKey(id);
			return aff;
		} finally {
			session.commit();
			session.close();
		}
	}
}
